<template>
  <div>
    <el-card shadow="never">
      <header-title title="商品结算"></header-title>
    </el-card>
    <el-card shadow="never">
      <div>
        <el-table :data="tableData" v-loading="loading" style="width: 100%">
          <el-table-column prop="businessName" label="商户名称"></el-table-column>
          <el-table-column prop="totalPrice" label="总金额(元)">
            <template slot-scope="scope">
              {{( scope.row.totalPrice / 100).toFixed(2)}}
            </template>
          </el-table-column>
          <el-table-column prop="count" label="订单交易数量"></el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button @click="settlement(scope.row)" type="text" style="color:#3dc2c2;">结算</el-button>
              <el-button @click="details(scope.row)" type="text" style="color:#71dc80;">详情</el-button>
            </template>
          </el-table-column>
          <div slot="empty"><img src="../../assets/blank_tip_page/blank_tip_page.png" alt=""></div>
        </el-table>
        <el-pagination
          :page-size="perPage"
          :current-page="page"
          :total="total"
          @current-change="changePage"
          style="margin-top: 15px;"
          background
          layout="prev, pager, next"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import HeaderTitle from "@/components/HeaderTitle"
import { mapState } from "vuex"
import * as commoditySettlement from "@/api/commoditySettlement.js"
export default {
  name: '',
  components: {
    HeaderTitle
  },
  data(){
    return {
      loading: true,
      tableData: [],
      perPage: 10, // 每页显示10条
      page: 1, // 当前页
      total: 0,
    }
  },
  computed:{
    ...mapState('app',['marketId'])
  },
  mounted(){
    this.getCSTableData()
  },
  watch: {
    '$store.state.app.marketId'(val, oldVal){
      this.getCSTableData()
    }
  },
  methods:{
    // 获取 列表
    getCSTableData() {
      commoditySettlement.getCSTableData(this.perPage, this.page)
        .then(res => {
          this.loading = false
          this.tableData = res.data.items
          this.total = res.data.total
        })
        .catch(error => {
          console.log(error)
          this.tableData = []
          this.total = 0
        })
    },
    changePage(num){
      this.page = num
      this.getCSTableData()
    },
    // 点击 结算
    settlement(row){
      const businessId = row.businessId
      const businessName = row.businessName
      const count = row.count
      const totalPrice = row.totalPrice
      this.$router.push({
        path: `settlement/${businessId}`,
        query: {
          businessId,
          businessName,
          count,
          totalPrice
        }
      })
    },
    // 点击 详情
    details(row){
      const businessId = row.businessId
      const businessName = row.businessName
      const count = row.count
      const totalPrice = row.totalPrice
      this.$router.push({
        path: `historySettlement/${businessId}`,
        query: {
          businessId,
          businessName,
          count,
          totalPrice
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  /deep/ .el-table__empty-text{
    width: 100%;
  }
.button {
  background: #64d9d6;
  color: #fff;
  margin-left: 40px;
}
</style>

